<?php

namespace app\adminapi\validate\setting\lang;

use app\common\service\ConfigService;
use app\common\validate\BaseValidate;
use app\common\model\lang\LangTranslate;

/**
 * 翻译验证器
 * Class TranslateValidate
 * @package app\adminapi\validate\setting
 */
class TranslateValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|number',
        'client_id' => 'require|number',
        'code' => 'require',
        'list' => 'require|array|checkList',
        'file' => 'require',
    ];

    protected $message = [
        'id.require' => '请输入id',
        'id.number' => 'id必须是数字',
        'client_id.require' => '请选择语言分类',
        'client_id.number' => '语言分类错误',
        'code.require' => '请输入翻译语句',
        'list.require' => '请添加译文',
        'list.array' => '译文必须为数组',
        'file.require' => '请选择要下载的文件',
    ];

    /**
     * @notes 添加场景
     * @return LangValidate
     */
    public function sceneAdd()
    {
        return $this->only(['client_id', 'type_id', 'code', 'list'])->append('code','checkCodeUnique');
    }

    /**
     * @notes 编辑场景
     * @return LangValidate
     */
    public function sceneEdit()
    {
        return $this->only(['client_id', 'type_id', 'code', 'list']);
    }

    /**
     * @notes 详情场景
     * @return LangValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id','code','client_id']);
    }

    /**
     * @notes 删除场景
     * @return LangValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id','code','client_id']);
    }

    /**
     * @notes 下载场景
     * @return LangValidate
     */
    public function sceneDownload()
    {
        return $this->only(['file']);
    }

    /**
     * @notes 翻译语句
     * @return TranslateValidate
     */
    public function sceneAutoTranslate()
    {
        return $this->only(['code'])
            ->append('checkConfig');
    }

    /**
     * @notes 验证数组
     * @param $list
     * @param $rule
     * @param $data
     * @return string
     */
    public function checkList($list, $rule, $data)
    {
        if (!empty($list)){
            foreach ($list as $item){
                if (!isset($item['type_id']) || !isset($item['after_text'])){
                    return '数据格式错误';
                }
            }
        }
        return true;
    }

    /**
     * @notes 验证数组
     * @return string|true
     */
    public function checkConfig($list, $rule, $data)
    {
        if (empty($data['lang_list'])){
            return '缺少翻译目标语言';
        }

        $defaultEngine = ConfigService::get('translate', 'engine', false);
        if($defaultEngine === false) {
            return '请开启翻译配置';
        }
        return true;
    }

    /**
     * @notes 重复验证
     * @return string|true
     */
    public function checkCodeUnique($list, $rule, $data)
    {
        $codeInfo = LangTranslate::where([
            ['client_id','=',$data['client_id']],
            ['code','=',$data['code']],
        ])->findOrEmpty();
        if (!$codeInfo->isEmpty()){
            return '翻译语句已存在';
        }
        return true;
    }

}